﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Rung.EBook.DocFormater.Umd;

namespace Rung.EBook.UI.Controls.Panels
{
    public partial class pnBookReader : UserControl
    {
        private WriteInfo mWriteInfo;
        private AppendInfo mAppendInfo;
        private UmdBookBM mBookBM;

        public pnBookReader()
        {
            InitializeComponent();
        }

        internal pnBookReader(WriteInfo infoWriter, AppendInfo appendInfo)
            : this()
        {
            this.mWriteInfo = infoWriter;
            this.mAppendInfo = appendInfo;
        }

        private void pnBookReader_Load(object sender, EventArgs e)
        {
            this.Dock = DockStyle.Fill;
        }

        private void btnBrowser_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = false;
            dialog.Filter = "UMD文件|*.umd";

            if (dialog.ShowDialog(this) == DialogResult.OK)
            {
                txbFileFullName.Text = dialog.FileName;
            }
        }

        ToolTip tooltip = new ToolTip();
        private void txbFileFullName_MouseEnter(object sender, EventArgs e)
        {
            tooltip.Show(txbFileFullName.Text, txbFileFullName);
        }

        private void txbFileFullName_MouseLeave(object sender, EventArgs e)
        {
            tooltip.Hide(txbFileFullName);
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txbFileFullName.Text)) return;

            this.mBookBM = UmdFormater.ReadUMDBook(txbFileFullName.Text);
            this.lbMenu_Bind();
        }

        /// <summary>
        /// （重新）绑定章节菜单的数据。
        /// </summary>
        private void lbMenu_Bind()
        {
            this.lbChapters.Items.Clear();

            foreach (ChapterBM cBM in this.mBookBM.Chapters)
                this.lbChapters.Items.Add(cBM);
        }

        /// <summary>
        /// 
        /// </summary>
        private void lbChapters_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbChapters.SelectedIndex < 0) return;

            string title = lbChapters.SelectedItem.ToString();
            txbContent.Text = this.mBookBM.Chapters.Find(delegate(ChapterBM bm) { return bm.Title == title; }).Content;
        }
    }
}
